/**
 * dao实现类生成器
 * @author dengzongrong
 * @version 1.1
 * @date 2011-10-09
 */
package com.lemon.ai.socket.packets.spiritgame.model.generator;

import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lemon.ai.socket.packets.spiritgame.model.db.MetaData;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Column;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Property;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Table;

public class DtoGenerator extends AbstractGenerator {

	private String PROPERTY_TEMP = "	private TYPE_NAME PROPERTY_NAME;\r";
	private String METHOD_TEMP = "	public void setMETHOD_NAME(TYPE_NAME PROPERTY_NAME) {\r"
			+ "		this.PROPERTY_NAME = PROPERTY_NAME;\r"
			+ "	}\r\r"
			+ "	public TYPE_NAME getMETHOD_NAME() {\r"
			+ "		return this.PROPERTY_NAME;\r" + "	}\r\r";

	private String PROPERTY_TEMP_ARRAYS = "	private TYPE_NAME [] PROPERTY_NAME = new TYPE_NAME[ARRAY_SIZE];\r";

	private String METHOD_TEMP_ARRAYS = "	public void setMETHOD_NAME(int index, TYPE_NAME value) {\r"
			+ "		this.PROPERTY_NAME[index] = value;\r"
			+ "	}\r\r"
			+ "	public TYPE_NAME getMETHOD_NAME(int index) {\r"
			+ "		return this.PROPERTY_NAME[index];\r" + "	}\r\r";

	public DtoGenerator() {
		super("DtoTemp.txt");
	}

	/**
	 * 根据表名生成Dto代码
	 * 
	 * @param tableName
	 *            表名
	 * @return Dto代码
	 */
	public String generate(Table table) {
		String[] temps = generatePropertiesAndMethods(table);
		return super.temp
				.replace("PACKAGE_NAME", super.prop.getProperty("package"))
				.replace("CLASS_NAME", generateClassName(table.getName()))
				.replace("IMPORTS", temps[0]).replace("PROPERTIES", temps[1])
				.replace("SETS_AND_GETS", temps[2])
				.replace("COPY_CONSTRACTOR", temps[3]);
	}

	public String generateFileName(Table table) {
		String className = generateClassName(table.getName());
		return super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				+ "\\domain\\" + className + ".java";
	}

	/**
	 * 根据表的信息，生成要替换的三个String，分别为imports, properties, methods
	 * 
	 * @param table
	 * @return new String[]{imports, properties, methods}
	 */
	public String[] generatePropertiesAndMethods(Table table) {
		String imports = "";
		String properties = "";
		String methods = "";
		String copy_constractor = "";// 复制构造函数

		boolean existDate = false;
		for (int i = 0; i < table.getColumns().size(); i++) {
			Property p = generatePropertyFromColumn(table.getColumns().get(i));
			if ("Date".equals(p.getType()))
				existDate = true;

			if(table.isInGroup(i)) {
				continue;
			}

			properties += PROPERTY_TEMP;

			properties = properties.replace("TYPE_NAME", p.getType()). // 成员类型
					replace("PROPERTY_NAME", p.getName()); // 成员名字
			copy_constractor += "\t\tthis." + p.getName() + " = c."
					+ p.getName() + ";\r";

			methods += METHOD_TEMP;
			methods = methods.replace("TYPE_NAME", p.getType())
					.replace("PROPERTY_NAME", p.getName())
					.replace("METHOD_NAME", generateClassName(p.getName()));

		}





		Map<String, List<Column>> s = table.getSameColumns();
		for (Entry<String, List<Column>> e : s.entrySet()) {

			Property p = generatePropertyFromColumn(e.getValue().get(0));

			properties += PROPERTY_TEMP_ARRAYS;
			
			properties = properties.replace("TYPE_NAME", p.getType()). // 成员类型
					replace("PROPERTY_NAME", getFieldName( e.getKey() )).
					replaceAll("ARRAY_SIZE", e.getValue().size() + ""); // 成员名字

			copy_constractor += "\t\tthis." + e.getKey() + " = Arrays.copyOf(c." + 
					e.getKey() + ", c." + 
					 e.getKey() + ".length);\r";

			
			methods += METHOD_TEMP_ARRAYS;
			methods = methods.replace("TYPE_NAME", p.getType())
					.replaceAll("PROPERTY_NAME", getFieldName(e.getKey()))
					.replaceAll("METHOD_NAME", generateClassName(getFieldName(e.getKey())))
					;

		}







		if (existDate)
			imports += "\rimport java.util.Date;\r";
		return new String[] { imports, properties, methods, copy_constractor };
	}

	private String getFieldName(String fieldName) {
		return fieldName.replaceAll("[0-9]", "");
	}

	/**
	 * 获取与table的第col列类似的所有列的数量
	 * 
	 * @param col
	 * @param table
	 * @return
	 */
	private String getArraySize(int col, Table table) {
		Column c = table.getColumns().get(col);
		String temp = getFieldName(c.getName());
		int size = 0;
		for (Column cl : table.getColumns()) {
			if (cl.getName().startsWith(temp)) {
				size++;
			}
		}
		return size + "";
	}

	/**
	 * 
	 * @param i
	 * @param table
	 * @return
	 */
	private int getIndex(int i, Table table) {
		Column c = table.getColumns().get(i);
		String temp = c.getName().replaceAll("[0-9]", "");

		int ind = 0;
		boolean isFind = false;
		for (Column it : table.getColumns()) {
			if (it.getName().startsWith(temp)) {
				ind++;
				isFind = true;
			}
		}
		if (isFind) {
			return ind - i;

		} else {
			return -1;
		}
	}

	public static void main(String[] args) {
		MetaData meta = MetaData.getInstance();
		List<Table> tables = meta.getTables();
		DtoGenerator g = new DtoGenerator();
		System.out.println(g.generate(tables.get(0)));
	}

}
